var express = require("express");
const {
  get,
  add,
  delTheaters,
  findTheaters,
  changeTheaters,
  findTheatersOne,
  searchone,
  findTheatersMore
} = require("../api/theaters");
//引入端口
var router = express.Router();

//数据查询 排片用的
router.get('/search', async function (req, res, next) {
    const { cinemasId } = req.query;
    const data = await searchone(cinemasId);
    res.send(data);
  });
//引入放映厅模型
// var {theatersModel}=require("../db/model/theaters");
// const {Model}=require("mongoose");
//get请求获取放映厅数据和页面相关的操作
router.get("/", async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await get({ curPage, eachPage });
  res.send(data);
});
//post请求新增放映厅数据/theaters
router.post("/", async function (req, res, next) {
  const { name, status, cinemasId } = req.body;
  const data = await add({
    name,
    status,
    cinemasId,
  });
  res.send(data);
});

// 删除放映厅
router.delete("/", async function (req, res, next) {
  const { _id } = req.body;
  const data = await delTheaters(_id);
  res.send(data);
});
// 获取影院对应的放映厅
router.get("/getCinemas", async function (req, res, next) {
  const { cinemasId } = req.query;
  const data = await findTheatersMore(cinemasId);
  res.send(data);
});
//分页获取放映厅数据
router.get("/", async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await findTheaters({ curPage, eachPage });
  res.send(data);
});
// 获取搜索的放映厅
router.get("/getOne", async function (req, res, next) {
  const { _id } = req.query;
  console.log(_id);
  const data = await findTheatersOne(_id);
  console.log(data);
  res.send(data);
});
// 修改放映厅数据
router.put("/", async function (req, res, next) {
  const theater = req.body;
  const data = await changeTheaters(theater);
  res.send(data);
});


//暴露接口
module.exports = router;
